package services;

import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;

import model.Constants;
import model.YouTubeEntry;

import com.google.gdata.client.media.ResumableGDataFileUploader;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.media.MediaSource;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.util.ServiceException;

public class YoutubeUploadService {

    private final YouTubeService youTubeService;
    private FileUploadProgressListener fileUploadProgressListener;

    public YoutubeUploadService(YouTubeService service) 
    {
    	this.youTubeService = service;	
    	this.fileUploadProgressListener = new FileUploadProgressListener();
    }

    /**
     * Uploads a new video to YouTube.
     *
     * @param service An authenticated YouTubeService object.
     * @throws IOException Problems reading user input.
     */
    public ResumableGDataFileUploader uploadVideo(YouTubeEntry youtubevideo)
            throws IOException, ServiceException, InterruptedException {
    	MediaFileSource mediasource = youtubevideo.getMediaSourceFile();
    	VideoEntry videoEntry = youtubevideo.getYouTubeVideoEntry();
        String title = youtubevideo.getVideotitle();
    	return new ResumableGDataFileUploader.Builder(this.youTubeService, new URL(Constants.RESUMABLE_UPLOAD_URL), mediasource, videoEntry).title(title).trackProgress(this.fileUploadProgressListener, Constants.PROGRESS_UPDATE_INTERVAL).chunkSize(Constants.DEFAULT_CHUNK_SIZE).build();

    }

	public FileUploadProgressListener getFileUploadProgressListener() {
		return fileUploadProgressListener;
	}
    

}